<?php
/*
KAP 0.3
composant de production de l'affichage

// cette implémentation repose sur la librairie noyau datatree
et sur le composant templatefeeder


file version 2004-12-09 16:18:37
*/

class wakpageManager extends wakCoreComponent {
/*----------------------------------------------------------------------------------------------
                                                                                      proprietes
----------------------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'default'=>array(
			'template'=>'default.template.xhtml',
			'path'=>'xhtml/'
		),
		'templatesMap'=> array(
			// uri => template
		)
	);
	var $template; // template a utiliser
	/*var $pageData=array(
		'head'=>array(),
		'body'=>array(),
	);*/
	var $data;
	var $pageThrown=false;
/*----------------------------------------------------------------------------------------------
                                                                                    constructeur
----------------------------------------------------------------------------------------------*/
	function wakpageManager(& $controller) {
		// appel du constructeur parent
		$const=get_parent_class($this);
		parent::$const($controller);
		// librairies
		//wakNeedClass('arraypath');
		$this->data=& wakNeedCoreObject('datatree');
		//$this->data->loadData(array('head'=>'','body'=>''));
		// $this->setTemplate($this->parameters['default']);
		// echo wakaffvar($this->parameters,'pagemanager-param');
	}
/*----------------------------------------------------------------------------------------------
                                                                              methodes publiques
----------------------------------------------------------------------------------------------*/
	// indique le template a utiliser
	function setTemplate($template, $path=NULL) {
		if ($path!==NULL and file_exists($path.$template)) $template=$path.$template;
		if (file_exists($this->parameters['default']['path'].$template)) $template=$this->parameters['default']['path'].$template;
		$this->template=$template;
	}
	
	// insère un contenu à l'emplacement indiqué
	function insert($content,$path='/') {
		wakLog("pageManager->insert : ".strlen($content)." chars to $path");
		return $this->data->add($path,$content);
	}
	
	function set($content,$path='/') {
		wakLog("pageManager->set : ".strlen($content)." chars to $path");
		return $this->data->set($path,$content);
	}
	
	function delete($path) {
		return $this->data->set($path,NULL);
	}
	
	function & getPage($file=NULL,$ignoreData=false) {
		// setting get vars to forward url string as a constant
		if (!defined('URL_FORWARD_VARS'))
			define('URL_FORWARD_VARS',wakUrlGetVarsString());
		// setting post vars to forward hidden fields as a constant
		if (!defined('POST_FORWARD_VARS_FIELDS'))
			define('POST_FORWARD_VARS_FIELDS',wakPostVarsHiddenFields());
		
		if (!$template=$this->getTemplate($file))
			trigger_error("wakPageManager: unable to retrieve template for the page");// echo $template;
	
		//echo "<p>wakPageManager: data to send to page: ".wakaffvar($this->data->get())."</p>";
		if ($ignoreData) $data=NULL; else $data=&$this->data;
		$r=wakTemplateFeed($template,$data);
		return $r;
	}
	
	function throwPage($page=NULL,$ignoreData=false) {
		if ($this->pageThrown) {
			trigger_error("wakPageManager->throwPage: page already thrown");
			return false;
		}
		if (!$page=& $this->getPage($page,$ignoreData)) return false;
		echo $page;
		$this->pageThrown=true;
		$size=strlen($page);
		wakLog("page sent, $size chars");
		return $size;
	}
/*----------------------------------------------------------------------------------------------
                                                                              methodes privees
----------------------------------------------------------------------------------------------*/
	function getTemplate($template=NULL,$path=NULL) {
		if (!$template)
			$template=$this->template;
		if (!$template)
			$template=$this->getUriTemplate(CALL_URI);// echo "<p>".CALL_URI." template = $template</p>";
		if (!$template)
			$template=$this->parameters['default']['template'];
		if (!$template)
			return false;
		if (strlen($template)<255 and file_exists($template))
			return $template;
		if (strlen($template)<255 and file_exists($this->parameters['default']['path'].$template))
			return $this->parameters['default']['path'].$template;
		return $template;
	}
	
	function getUriTemplate($uri) {
		while ($uri) {
			//echo "<p>searching for $uri template</p>";
			if (isset($this->parameters['templatesMap'][$uri])) {
				//echo wakaffvar($this->parameters['templatesMap'][$uri],"template for $uri");
				return $this->parameters['templatesMap'][$uri];
			}
			// raccourcissement
			$uri=substr($uri,0,strrpos($uri,'/'));
		}
		return false;
	}
}
?>
